package com.bobo.web.provider.config;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * yangbo
 * 2019-11-20
 * basketball-web
 **/
@Aspect
@Component
public class DataSourceAop {
    /**
     * 基于注解
     */

    @Pointcut("@annotation(com.bobo.web.api.annotation.Slave) ")
    public void readPointcut() {

    }

    @Pointcut("@annotation(com.bobo.web.api.annotation.Master) ")
    public void writePointcut() {

    }

    /**
     * 基于execution表达式
     */
//    @Pointcut("!@annotation(com.bobo.web.api.annotation.Master) " +
//            "&& (execution(* com.bobo.web.provider.service..*.select*(..)) " +
//            "|| execution(* com.bobo.web.provider.service..*.get*(..)))")
//    public void readPointcut() {
//
//    }
//
//    @Pointcut("@annotation(com.bobo.web.api.annotation.Master) " +
//            "|| execution(* com.bobo.web.provider.service..*.insert*(..)) " +
//            "|| execution(* com.bobo.web.provider.service..*.add*(..)) " +
//            "|| execution(* com.bobo.web.provider.service..*.update*(..)) " +
//            "|| execution(* com.bobo.web.provider.service..*.edit*(..)) " +
//            "|| execution(* com.bobo.web.provider.service..*.delete*(..)) " +
//            "|| execution(* com.bobo.web.provider.service..*.remove*(..))")
//    public void writePointcut() {
//
//    }

    @Before("readPointcut()")
    public void read() {
        DBContextHolder.slave();
    }

    @Before("writePointcut()")
    public void write() {
        DBContextHolder.master();
    }
}
